﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Charades.Web.Server.User
{
	public class UserHandler
	{
		private ConcurrentDictionary<string, User> _userList;

		public UserHandler()
		{
			_userList = new ConcurrentDictionary<string, User>();
		}

		public void AddUser(User user)
		{
			_userList.TryAdd(user.ConnectionId, user);
		}

		public void RemoveUser(string connectionId)
		{
			User u;
			_userList.TryRemove(connectionId, out u);
		}

		public User GetUser(string connectionId)
		{
			return _userList.ContainsKey(connectionId) ? _userList[connectionId] : null;
		}

		public List<User> GetUsers()
		{
			return _userList.Values.ToList();
		}

		public int GetUsersCount()
		{
			return _userList.Values.Count;
		}

		public ICollection<string> GetUserConnectionIds()
		{
			return _userList.Keys;
		}

		public bool UserExists(string connectionId)
		{
			return _userList.ContainsKey(connectionId);
		}
	}
}